package com.next

import grails.converters.JSON
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*
import grails.gorm.transactions.Transactional

/**
 **/
@Secured(['ROLE_ADMINISTRATOR', 'ROLE_CUSTOMER_SERVICE_REPRESENTATIVE', 'ROLE_ACCOUNT_MANAGER', 'ROLE_COMPANY_ADMINISTRATOR', 'ROLE_GENERAL_RISK_MANAGER', 'ROLE_BRANCH_GENERAL_MANAGER', 'ROLE_BRANCH_OFFICE_RISK_MANAGER', 'ROLE_HEAD_OFFICE_RISK_MANAGER', 'ROLE_GENERAL_MANAGER', 'ROLE_CRO', 'ROLE_CEO', 'ROLE_POST_LOAN_MANAGER', 'ROLE_BRANCH_OFFICE_POST_LOAN_MANAGER'])
@Transactional(readOnly = true)
class PropertyInformationController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index()
    {
        params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
        params.offset = params.offset ? params.offset.toInteger() : 0
        def max = params.max
        def offset = params.offset
        def sort = params["sort"]
        def order = params["order"]
        def account = params["account"]
        def searchSql = " from PropertyInformation p where 1=1 "
        def sql = " "
        if (account)
        {
            sql += " and p.account.name like '%${account}%'"
        }
        def countSql = " select count(p.id) from PropertyInformation p where 1=1 " + sql
        if (order && sort)
        {
            sql += " order by p.${sort} ${order}"
        } else
        {
            sql += "order by p.modifiedDate desc "
        }
        searchSql += sql
        def propertyInformationList = PropertyInformation.executeQuery(searchSql, [max: max, offset: offset])
        def count = PropertyInformation.executeQuery(countSql)


        respond propertyInformationList, model: [propertyInformationCount: count[0], params: params]
    }

    def show(PropertyInformation propertyInformation)
    {
        respond propertyInformation
    }

    def create()
    {
        def targetUri = request.getHeader("referer")
        def accountId = params.account
        respond new PropertyInformation(params), model: [accountId: accountId, targetUri: targetUri]
    }

    /**
     * 生成权利证书号*/
    @Transactional
    def createCertificateNumber()
    {
        def serialNumber = params["serialNumber"]
        def accountId = params["account"]
        def name = "房产证"
        def account = Account.findById(accountId)
        def certificateType = CertificateType.findByName("固定资产")
        if (!certificateType)
        {
            certificateType = new CertificateType()
            certificateType.name = "固定资产"
            certificateType.save()
        }
        def certificate = Certificate.findBySerialNumber(serialNumber)
        if (!certificate)
        {
            certificate = new Certificate()
            certificate.name = name
            certificate.account = account
            certificate.type = certificateType
            certificate.serialNumber = serialNumber
            certificate.save()
        }
        render([certificate: certificate] as JSON)
    }

    @Transactional
    def save(PropertyInformation propertyInformation)
    {
        if (propertyInformation == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }
        if (propertyInformation.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond propertyInformation.errors, model: [targetUri: params['targetUri'], accountId: propertyInformation?.account?.id], view: 'create'
            return
        }
        def certificateId = params["certificate.id"]
        def certificate = Certificate.findById(certificateId)
        propertyInformation.certificate = certificate

        propertyInformation.save flush: true


        redirect controller: "account", action: "show1", method: "GET", id: propertyInformation?.account?.id
    }

    def edit(PropertyInformation propertyInformation)
    {
        def targetUri = request.getHeader("referer")
        respond propertyInformation, model: [targetUri: targetUri]
    }

    @Transactional
    def update(PropertyInformation propertyInformation)
    {
        if (propertyInformation == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (propertyInformation.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond propertyInformation.errors, model: [targetUri: params['targetUri']], view: 'edit'
            return
        }
        def certificateId = params["certificate.id"]
        def certificate = Certificate.findById(certificateId)
        propertyInformation.certificate = certificate

        propertyInformation.save flush: true
        redirect controller: "account", action: "show1", method: "GET", id: propertyInformation?.account?.id
    }

    @Transactional
    def delete(PropertyInformation propertyInformation)
    {

        if (propertyInformation == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        propertyInformation.delete flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'propertyInformation.label', default: 'PropertyInformation'), propertyInformation.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'propertyInformation.label', default: 'PropertyInformation'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }
}
